﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

public partial class _1_2 : System.Web.UI.Page
{
    public string DisplayStr()
    {

        YingYongHanShu yyhs = new YingYongHanShu();

        return yyhs.DisplayStr2("101102");
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        

        HiddenField[] hb = new HiddenField[] { this.HiddenField1, this.HiddenField2, this.HiddenField3, this.HiddenField4, this.HiddenField5, this.HiddenField6, this.HiddenField7 };
        TextBox[] tbxZLSS = new TextBox[] { this.TextBoxZLSS1, this.TextBoxZLSS2, this.TextBoxZLSS3, this.TextBoxZLSS4, this.TextBoxZLSS5, this.TextBoxZLSS6, this.TextBoxZLSS7 };
        DropDownList[] ddlWRLB = new DropDownList[] { this.DropDownListWRLB1, this.DropDownListWRLB2, this.DropDownListWRLB3, this.DropDownListWRLB4, this.DropDownListWRLB5, this.DropDownListWRLB6, this.DropDownListWRLB7 };
        DropDownList[] ddlCLFF = new DropDownList[] { this.DropDownListCLFF1, this.DropDownListCLFF2, this.DropDownListCLFF3, this.DropDownListCLFF4, this.DropDownListCLFF5, this.DropDownListCLFF6, this.DropDownListCLFF7 };
        TextBox[] tbxSJCLNL = new TextBox[] { this.TextBoxSJCLNL1, this.TextBoxSJCLNL2, this.TextBoxSJCLNL3, this.TextBoxSJCLNL4, this.TextBoxSJCLNL5, this.TextBoxSJCLNL6, this.TextBoxSJCLNL7 };
        TextBox[] tbxCLL = new TextBox[] { this.TextBoxCLL1, this.TextBoxCLL2, this.TextBoxCLL3, this.TextBoxCLL4, this.TextBoxCLL5, this.TextBoxCLL6, this.TextBoxCLL7 };
        DropDownList[] ddlPFK = new DropDownList[] { this.DropDownListPFK1, this.DropDownListPFK2, this.DropDownListPFK3, this.DropDownListPFK4, this.DropDownListPFK5, this.DropDownListPFK6, this.DropDownListPFK7 };
        TextBox[] tbxYXTS = new TextBox[] { this.TextBoxYXTS1, this.TextBoxYXTS2, this.TextBoxYXTS3, this.TextBoxYXTS4, this.TextBoxYXTS5, this.TextBoxYXTS6, this.TextBoxYXTS7 };
        TextBox[] tbxyxfy = new TextBox[] { this.TextBoxyxfy1, this.TextBoxyxfy2, this.TextBoxyxfy3, this.TextBoxyxfy4, this.TextBoxyxfy5, this.TextBoxyxfy6, this.TextBoxyxfy7 };
        TextBox[] tbxTRSYRQ = new TextBox[] { this.TextBoxTRSYRQ1, this.TextBoxTRSYRQ2, this.TextBoxTRSYRQ3, this.TextBoxTRSYRQ4, this.TextBoxTRSYRQ5, this.TextBoxTRSYRQ6, this.TextBoxTRSYRQ7 };
        if (!IsPostBack)
        {
            LabelTiShi.Visible = false;

            foreach (var h in hb)
            {
                h.Value = "";
            }

            using (DataClassesDataContext mydata = new DataClassesDataContext())
            {
                for (int j = 0; j < 7; j++)
                {
                    ddlWRLB[j].DataSource = from w in mydata.wrlb
                                            select w;
                    ddlWRLB[j].DataTextField = "P_name";
                    ddlWRLB[j].DataValueField = "p_type";
                    ddlWRLB[j].DataBind();
                }

                var rd = from a in mydata.A01102
                         where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                         where a.DATA_MONTH == Convert.ToString(Session["ShenBaoNianDu"])
                         select new
                         {
                             zlss_mc = a.FACILITY_NAME == null ? "" : a.FACILITY_NAME,
                             zlss_id = a.FACILITY_ID,
                             wrlb = Convert.ToString(a.P_TYPE),
                             clff = a.METHOD_CODE == null ? "" : a.METHOD_CODE,
                             sjclnl = a.PLAN_COUNT, //== null ? "" : Convert.ToString(a.PLAN_COUNT),
                             cll = a.DEAL_CNT,// == null ? "" : Convert.ToString(a.DEAL_CNT),
                             SERIAL = a.SERIAL == null ? "" : Convert.ToString(a.SERIAL),//("编号： " + Convert.ToString(f.OUTPUT_CODE) + "名称:  " + Convert.ToString(f.OUT_NAME)),
                             yxts = a.USE_DAYS,// == null ? "" : Convert.ToString(a.USE_DAYS),
                             yxfy = a.USE_FEE,// == null ? "" : Convert.ToString(a.USE_FEE),
                             trsyrq = a.FOUND_DATE,// == null ? "" : Convert.ToString(a.FOUND_DATE),
                             suoding = a.suoding == null ? false : Convert.ToBoolean(a.suoding)
                         };
                int i = 0;
                foreach (var r in rd)
                {
                    hb[i].Value = r.zlss_id;
                    tbxZLSS[i].Text = r.zlss_mc;
                    ddlWRLB[i].SelectedIndex = ddlWRLB[i].Items.IndexOf(ddlWRLB[i].Items.FindByValue(r.wrlb));//r.wrlb;
                    ChuShiHuaXiaLaKuang(i);
                    ddlCLFF[i].SelectedValue = r.clff;
                    tbxSJCLNL[i].Text = r.sjclnl == null ? "" : string.Format("{0:#.####}", r.sjclnl);
                    tbxCLL[i].Text = r.cll == null ? "" : string.Format("{0:#.####}", r.cll);
                    ddlPFK[i].SelectedValue = r.SERIAL;
                    tbxYXTS[i].Text = r.yxts == null ? "" : Convert.ToString(r.yxts);
                    tbxyxfy[i].Text = r.yxfy == null ? "" : string.Format("{0:#.####}", r.yxfy);
                    tbxTRSYRQ[i].Text = r.trsyrq == null ? "" : string.Format("{0:yyyy-mm-dd}", r.trsyrq);
                    i++;
                    if (i >= 7) break;
                }
            }
        }
    }

    protected void TextBoxZLSS1_TextChanged(object sender, EventArgs e)
    {
        TextBoxCLL1.Text = TextBoxZLSS1.Text;
    }
    protected void ButtonTiJiao_Click(object sender, EventArgs e)
    {
        HiddenField[] hb = new HiddenField[] { this.HiddenField1, this.HiddenField2, this.HiddenField3, this.HiddenField4, this.HiddenField5, this.HiddenField6, this.HiddenField7 };
        TextBox[] tbxZLSS = new TextBox[] { this.TextBoxZLSS1, this.TextBoxZLSS2, this.TextBoxZLSS3, this.TextBoxZLSS4, this.TextBoxZLSS5, this.TextBoxZLSS6, this.TextBoxZLSS7 };
        DropDownList[] ddlWRLB = new DropDownList[] { this.DropDownListWRLB1, this.DropDownListWRLB2, this.DropDownListWRLB3, this.DropDownListWRLB4, this.DropDownListWRLB5, this.DropDownListWRLB6, this.DropDownListWRLB7 };
        DropDownList[] ddlCLFF = new DropDownList[] { this.DropDownListCLFF1, this.DropDownListCLFF2, this.DropDownListCLFF3, this.DropDownListCLFF4, this.DropDownListCLFF5, this.DropDownListCLFF6, this.DropDownListCLFF7 };
        TextBox[] tbxSJCLNL = new TextBox[] { this.TextBoxSJCLNL1, this.TextBoxSJCLNL2, this.TextBoxSJCLNL3, this.TextBoxSJCLNL4, this.TextBoxSJCLNL5, this.TextBoxSJCLNL6, this.TextBoxSJCLNL7 };
        TextBox[] tbxCLL = new TextBox[] { this.TextBoxCLL1, this.TextBoxCLL2, this.TextBoxCLL3, this.TextBoxCLL4, this.TextBoxCLL5, this.TextBoxCLL6, this.TextBoxCLL7 };
        DropDownList[] ddlPFK = new DropDownList[] { this.DropDownListPFK1, this.DropDownListPFK2, this.DropDownListPFK3, this.DropDownListPFK4, this.DropDownListPFK5, this.DropDownListPFK6, this.DropDownListPFK7 };
        TextBox[] tbxYXTS = new TextBox[] { this.TextBoxYXTS1, this.TextBoxYXTS2, this.TextBoxYXTS3, this.TextBoxYXTS4, this.TextBoxYXTS5, this.TextBoxYXTS6, this.TextBoxYXTS7 };
        TextBox[] tbxyxfy = new TextBox[] { this.TextBoxyxfy1, this.TextBoxyxfy2, this.TextBoxyxfy3, this.TextBoxyxfy4, this.TextBoxyxfy5, this.TextBoxyxfy6, this.TextBoxyxfy7 };
        TextBox[] tbxTRSYRQ = new TextBox[] { this.TextBoxTRSYRQ1, this.TextBoxTRSYRQ2, this.TextBoxTRSYRQ3, this.TextBoxTRSYRQ4, this.TextBoxTRSYRQ5, this.TextBoxTRSYRQ6, this.TextBoxTRSYRQ7 };
        ////**********逻辑判断***********
        ////判断 1、治理设施和污染类别不能为空;
        ////                      
        ////          2、

        string strCuoWu = "提交内容有错误：";//记录错误内容
        int CuoWuXuHao = 0;//错误序号
        bool TuiChu = false; //记录逻辑判断过程中是否有错误需要中断退出提交过程
        TextBoxCuoWuXinXi.Text = "";
        for (int i = 0; i < 7; i++)
        {
            if (hb[i].Value == "")
            {
                if (!(tbxZLSS[i].Text.Trim() == "" && ddlWRLB[i].Text.Trim() == ""))
                {
                    if (tbxZLSS[i].Text.Trim() == "" || ddlWRLB[i].Text.Trim() == "")
                    {
                        TuiChu = true;
                        CuoWuXuHao++;
                        strCuoWu = strCuoWu + Convert.ToString(CuoWuXuHao) + "、 治理设施（" + Convert.ToString(i + 1) + "）排放口名称和污染类别不能为空。";
                    }
                }

            }
        }


        LabelTiShi.Text = "正在提交中，请不要关闭或离开...";
        LabelTiShi.Visible = true;


        if (Page.IsValid) //当所有校验控件都正确是才提交
        {

            LabelTiShi.Text = "正在提交中，请不要关闭或离开...";
            LabelTiShi.Visible = true;

            using (DataClassesDataContext mydata = new DataClassesDataContext())
            {

                //得到新的FACILITY_ID
                string zlssbh = "002";//FACILITY_ID初值

                var max = (from a in mydata.A01102
                           where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                           where a.DATA_MONTH == Convert.ToString(Session["ShenBaoNianDu"])
                           select a.FACILITY_ID).Max();
                if (!(max == null))
                {
                    zlssbh = string.Format("{0:D3}", Convert.ToInt32(max) + 2);
                }

                for (int i = 0; i < 7; i++)
                {
                    if (hb[i].Value == "")//hb[i]记录治理设施编号FACILITY_ID，新增治理设施
                    {
                        if (tbxZLSS[i].Text.Trim() != "" && ddlWRLB[i].SelectedValue != "")
                        {


                            A01102 p = new A01102();
                            p.FACILITY_ID = zlssbh;
                            p.FACILITY_NAME = tbxZLSS[i].Text.Trim();
                            p.P_TYPE = Convert.ToInt32(ddlWRLB[i].SelectedValue);
                            if (ddlCLFF[i].SelectedValue != "")
                            {
                                p.METHOD_CODE = ddlCLFF[i].SelectedValue;
                            }
                            p.PLAN_COUNT = tbxSJCLNL[i].Text.Trim() == "" ? 0 : Convert.ToDouble(tbxSJCLNL[i].Text.Trim());
                            p.DEAL_CNT = tbxCLL[i].Text.Trim() == "" ? 0 : Convert.ToDouble(tbxCLL[i].Text.Trim());
                            if (ddlPFK[i].SelectedValue != "")
                            {
                                p.SERIAL = Convert.ToInt32(ddlPFK[i].SelectedValue);
                            }
                            else
                            {
                                p.SERIAL = -1;//确保该值在数据库里不为空，该-1值不能导入排污收费系统
                            }
                            p.USE_DAYS = tbxYXTS[i].Text.Trim() == "" ? 0 : Convert.ToInt32(tbxYXTS[i].Text.Trim());
                            p.USE_FEE = tbxyxfy[i].Text.Trim() == "" ? 0 : Convert.ToDouble(tbxyxfy[i].Text.Trim());
                            if (tbxTRSYRQ[i].Text.Trim() != "")
                            {
                                p.FOUND_DATE = DateTime.ParseExact(tbxTRSYRQ[i].Text.Trim(), "yyyy-mm-dd", System.Globalization.CultureInfo.CurrentCulture);
                            }
                            p.qiyeid = Convert.ToInt32(Session["qiyeid"]);
                            p.DATA_MONTH = Convert.ToString(Session["ShenBaoNianDu"]);
                            mydata.A01102.InsertOnSubmit(p);

                            hb[i].Value = zlssbh;
                            zlssbh = string.Format("{0:D3}", Convert.ToInt32(zlssbh) + 2);//因为这里是循环结束后才提交给服务器，所以生成新的FACILITY_ID，给下一条新增记录用
                        }
                    }
                    else//修改或删除治理设施
                    {
                        var rd = (from a in mydata.A01102
                                  where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                                  where a.DATA_MONTH == Convert.ToString(Session["ShenBaoNianDu"])
                                  where a.FACILITY_ID == Convert.ToString(hb[i].Value)
                                  select a).FirstOrDefault();

                        if (tbxZLSS[i].Text.Trim() == "")//当治理设施名称是空时，删除该治理设施记录
                        {
                            if (rd != null)
                            {
                                mydata.A01102.DeleteOnSubmit(rd);
                                ddlWRLB[i].SelectedIndex = -1;
                                ddlCLFF[i].Items.Clear();
                                ddlPFK[i].Items.Clear();
                                tbxSJCLNL[i].Text = "";
                                tbxCLL[i].Text = "";
                                tbxYXTS[i].Text = "";
                                tbxyxfy[i].Text = "";
                                tbxTRSYRQ[i].Text = "";
                            }
                        }
                        else//修改
                        {
                            if (rd != null)
                            {
                                rd.FACILITY_NAME = tbxZLSS[i].Text.Trim();
                                rd.P_TYPE = Convert.ToInt32(ddlWRLB[i].SelectedValue);
                                if (ddlCLFF[i].SelectedValue != "")
                                {
                                    rd.METHOD_CODE = ddlCLFF[i].SelectedValue;
                                }
                                rd.PLAN_COUNT = tbxSJCLNL[i].Text.Trim() == "" ? 0 : Convert.ToDouble(tbxSJCLNL[i].Text.Trim());
                                rd.DEAL_CNT = tbxCLL[i].Text.Trim() == "" ? 0 : Convert.ToDouble(tbxCLL[i].Text.Trim());
                                if (ddlPFK[i].SelectedValue != "")
                                {
                                    rd.SERIAL = Convert.ToInt32(ddlPFK[i].SelectedValue);
                                }
                                rd.USE_DAYS = tbxYXTS[i].Text.Trim() == "" ? 0 : Convert.ToInt32(tbxYXTS[i].Text.Trim());
                                rd.USE_FEE = tbxyxfy[i].Text.Trim() == "" ? 0 : Convert.ToDouble(tbxyxfy[i].Text.Trim());
                                if (tbxTRSYRQ[i].Text.Trim() != "")
                                {
                                    rd.FOUND_DATE = DateTime.ParseExact(tbxTRSYRQ[i].Text.Trim(), "yyyy-mm-dd", System.Globalization.CultureInfo.CurrentCulture);
                                }
                            }

                        }
                    }
                }
                mydata.SubmitChanges();
            }
            LabelTiShi.Visible = false;

            if (TuiChu)
            {
                Response.Write("<script>alert('部分提交内容有错误，未能全部提交，请检查!')</script>");
                TextBoxCuoWuXinXi.Text = strCuoWu;
            }
            else
            {
                Response.Write("<script>alert('你所填写申报内容正确，已成功提交!')</script>");
            }

        }

    }

    public void ChuShiHuaXiaLaKuang(int xh)
    {
        DropDownList[] ddlWRLB = new DropDownList[] { this.DropDownListWRLB1, this.DropDownListWRLB2, this.DropDownListWRLB3, this.DropDownListWRLB4, this.DropDownListWRLB5, this.DropDownListWRLB6, this.DropDownListWRLB7 };
        DropDownList[] ddlCLFF = new DropDownList[] { this.DropDownListCLFF1, this.DropDownListCLFF2, this.DropDownListCLFF3, this.DropDownListCLFF4, this.DropDownListCLFF5, this.DropDownListCLFF6, this.DropDownListCLFF7 };
        DropDownList[] ddlPFK = new DropDownList[] { this.DropDownListPFK1, this.DropDownListPFK2, this.DropDownListPFK3, this.DropDownListPFK4, this.DropDownListPFK5, this.DropDownListPFK6, this.DropDownListPFK7 };
        using (DataClassesDataContext mydata = new DataClassesDataContext())
        {
            if (ddlWRLB[xh].SelectedValue != "")
            {
                ddlCLFF[xh].DataSource = from c in mydata.CODE_CLEAN
                                         where c.P_TYPE == Convert.ToChar(ddlWRLB[xh].SelectedValue)
                                         select c;
                ddlCLFF[xh].DataTextField = "SF_NAME";
                ddlCLFF[xh].DataValueField = "P_TYPE";
                ddlCLFF[xh].Items.Clear();
                ddlCLFF[xh].DataBind();

                ddlPFK[xh].DataSource = from f in mydata.F_OUTPUT
                                        where f.qiyeid == Convert.ToInt32(Session["qiyeid"])
                                        where f.POLLUTE_TYPE == Convert.ToChar(ddlWRLB[xh].SelectedValue)
                                        where f.LOGOUT_FLAG == 0
                                        where f.SERIAL < 1000
                                        select new
                                        {
                                            mb = "编号: " + f.OUTPUT_CODE + "   名称:" + f.OUT_NAME,
                                            serial = f.SERIAL
                                        };
                ddlPFK[xh].DataTextField = "mb";
                ddlPFK[xh].DataValueField = "serial";
                ddlPFK[xh].Items.Clear();
                ddlPFK[xh].DataBind();
            }
            else
            {
                ddlCLFF[xh].Items.Clear();
                ddlPFK[xh].Items.Clear();
            }
        }
    }

    protected void DropDownListWRLB1_SelectedIndexChanged(object sender, EventArgs e)
    {
        ChuShiHuaXiaLaKuang(0);
    }
    protected void DropDownListWRLB2_SelectedIndexChanged(object sender, EventArgs e)
    {
        ChuShiHuaXiaLaKuang(1);

    }
    protected void DropDownListWRLB3_SelectedIndexChanged(object sender, EventArgs e)
    {
        ChuShiHuaXiaLaKuang(2);

    }
    protected void DropDownListWRLB4_SelectedIndexChanged(object sender, EventArgs e)
    {
        ChuShiHuaXiaLaKuang(3);

    }
    protected void DropDownListWRLB5_SelectedIndexChanged(object sender, EventArgs e)
    {
        ChuShiHuaXiaLaKuang(4);

    }
    protected void DropDownListWRLB6_SelectedIndexChanged(object sender, EventArgs e)
    {
        ChuShiHuaXiaLaKuang(5);

    }
    protected void DropDownListWRLB7_SelectedIndexChanged(object sender, EventArgs e)
    {
        ChuShiHuaXiaLaKuang(6);

    }
}